using System.Globalization;
using System.Reflection;
using System.Text;
using AspNetCore.Localizer.Json.Commons;
using AspNetCore.Localizer.Json.Extensions;
using AspNetCore.Localizer.Json.JsonOptions;
using AspNetCore.Localizer.Json.Localizer;
using Localization.Shared;
using Microsoft.AspNetCore.Localization;

var builder = WebApplication.CreateBuilder(args);

var supportedCultures = new HashSet<CultureInfo>
{
	new ("en-US"), 
	new ("fr-FR"), 
};
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
	options.DefaultRequestCulture = new RequestCulture("en-US");
	options.SupportedCultures = supportedCultures.ToList();
	options.SupportedUICultures = supportedCultures.ToList();
});
builder.Services.AddJsonLocalization(options =>
{
	options.LocalizationMode = LocalizationMode.Basic; 
	options.LocalizerDiagnosticMode = false;
	options.CacheDuration = TimeSpan.FromMinutes(1);
	options.SupportedCultureInfos = supportedCultures;
	options.FileEncoding = Encoding.UTF8;
	options.DefaultCulture = new CultureInfo("en-US");
	options.AssemblyHelper = new AssemblyHelper(Assembly.GetExecutingAssembly());
});
var app = builder.Build();

app.UseRequestLocalization();
app.UseHttpsRedirection();
app.MapGet("/articles/{id}", (
	int id, 
	IJsonStringLocalizer localizer
) => 
{
	var article = Data.GetArticles().FirstOrDefault(a => a.Id == id);
	if (article == null)
	{
		return Results.NotFound();
	}
	var articleDto = new ArticleDto
	{
		Name = localizer[article.NameKey],
		Description = localizer[article.DescriptionKey]
	};
	return Results.Ok(articleDto);
});
app.Run();

